﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Public.Tools
{
    public sealed class Setting
    {
        static Setting instance = null;
        static readonly object padlock = new object();

 
        private IDictionary<string, IList<SysSetting>> messagelist;
        private Setting()
        {
           

            #region 消息配置
            string messageconfigfile = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath + "/ErrorMessage.config");
            XmlDocument messagexmldoc = new XmlDocument();
            messagexmldoc.Load(messageconfigfile);

            XmlNamespaceManager messagensmgr = new XmlNamespaceManager(messagexmldoc.NameTable);
            messagensmgr.AddNamespace("ErrorMessage", "ErrorMessage");
            XmlNodeList message_sys_sNodes = messagexmldoc.SelectNodes("//ErrorMessage:Region", messagensmgr);
            messagelist = new Dictionary<string, IList<SysSetting>>();
            foreach (XmlNode ssNode in message_sys_sNodes)
            {
                messagelist.Add(ssNode.Attributes["name"].Value, getNodesToObj(ssNode.ChildNodes));
            }
          

            #endregion
        }

        /// <summary>
        /// 获取所有节点中的键值转换成SysSetting对象
        /// </summary>
        /// <param name="nodeList">节点集合</param>
        /// <returns></returns>
        private IList<SysSetting> getNodesToObj(XmlNodeList nodeList)
        {
            IList<SysSetting> settings=new List<SysSetting>();
            string key,value;
            foreach (XmlNode ssNode in nodeList)
            {
                key = ssNode.Attributes["key"].Value;
                value = ssNode.Attributes["value"].Value;
                settings.Add(new SysSetting(key, value));
            }
            return settings;
        }

        /// <summary>
        /// 得到Message对象的全局访问点方法 
        /// 线程安全的单例模式
        /// </summary>
        public static Setting Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Setting();
                    }
                    return instance;
                }
            }
        }

        /// <summary>
        /// 根据键获取值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>返回根据键找到的设置值</returns>
        public SysSetting getMessageByKey(string key)
        {
            SysSetting sys_s = null;
            try
            {
                if (string.IsNullOrEmpty(key))
                {
                    sys_s = messagelist["Error"][0];
                }
                else
                {
                    sys_s = messagelist["Error"].First(x => x.Key == key);
                }
            }
            catch (Exception ex)
            {
                sys_s = null;
            }
            return sys_s;
        }

        /// <summary>
        /// 重新加载配置文件
        /// </summary>
        public static void Reload()
        {
            lock (padlock)
            {
                instance = null;
                instance = new Setting();
            }
        }
    }

    /// <summary>
    /// 系统设置对象
    /// </summary>
    public class SysSetting
    {
        public SysSetting() { }
        public SysSetting(string key, string value)
        {
            Key = key;
            Value = value;
        }
        /// <summary>
        /// 键值
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }

    }
}